<?php
namespace app\home\controller;
use app\common\controller\AuthBase;
use app\home\model\Authrule;
use think\Request;
use think\Db;
/**
 * @title 节点管理
 * @description 接口说明
 * @group AUTH权限
 */
class Menu extends AuthBase{
    function _initialize(){
        parent::_initialize();
    }

    public function get(){
        $id = input('id');
        if($id>0){
            $db = new Authrule();
            $data = $db->find($id);
            return $data;
        }
    }


    public function getlist(){
        $model = new Authrule();
        $data=$model
            ->field("
					id,
					fid as pid,
				   name as node,
				   ismenu,
					title as name,sort
				")
            ->where(array("status"=>"1","deleted"=>0))->order("sort asc")->select()->toArray();

        $data = $this->tree($data,'id','pid');//dump($data);die;
        return json(['data' => $data]);
    }


    public function add(){

        if ($this->request->isPost()){
            $db = new Authrule();
            $param = input();
            $inserid = $db->save($param);
            $data = $db->where("status=1 AND id=$inserid")->find();
            return json(['msg'=>'增加成功','code'=>0]);
        }
    }


    public function put(){

        if ($this->request->isPost()){
            $db = new Authrule();
            $data = input();
            $db->save($data,['id'=>$data['id']]);
            return json(['msg'=>'更新成功','code'=>0]);
        }

    }


    public function cancel(){
        $id = input('id');
        if ($this->request->isPost()){
            $db = new Authrule();

            if ($id<=0){
                $this->error("要删除的ID不能为空");
            }else{
                //删除当前层级
                $setstatus = $db->where(array('id'=>$id))->setField('deleted',time());
                $list = $db->where("1=1")->select();
                $lists = $this->catecree($list,$id,1);
                foreach ($lists as $v){
                    if($v['status'] == 1){
                        $del = $db->where(array('id'=>$v['id']))->setField('deleted',time());
                    }
                }
                if($setstatus){
                    return json(['msg'=>'删除成功','code'=>0]);
                    //$this->success('删除成功');
                }else{
                    return json(['msg'=>'更新成功','code'=>1]);
                    //$this->error('删除成功');
                }
            }
        }
    }

    private function catecree($list,$fid=0,$level=1){
        $arr = [];
        foreach($list as $l){
            if($l['fid']==$fid){
                $l['level']=$level;
                $arr[] = $l;
                $child=$this->catecree($list,$l['id'],$level+1);
                if(is_array($child)){
                    $arr=array_merge($arr,$child);
                }
            }
        }
        return $arr;
    }

    /*
        调整顺序层次
    */
    public function sort(){
        $id = input('id');
        $db = new Authrule();
        $data=input("post.");

        $target_data=$db->find($data["targetId"]);

        $target_fid=$target_data["fid"];
        $target_sort=$target_data["sort"];

        switch ($data["moveType"])
        {
            case "next":
                $sortdata["sort"]=$target_sort+1;
                $sortdata["fid"]=$target_fid;
                break;
            case "prev":
                $sortdata["sort"]=$target_sort-1;
                $sortdata["fid"]=$target_fid;
                break;
            case "inner":
                $sortdata["fid"]=$data["targetId"];
                break;
            default:
                $this->error("参数错误！");
        }

        $result = $db->where(array("id"=>$data["id"]))->save($sortdata);
        //echo $db->_sql();
        if($result>0){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }

    }

}